package com.xy.Servlet;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xy.model.Admin;
import com.xy.model.DeliverToStudent;
import com.xy.model.DeliverToTeacher;
import com.xy.model.TextBook;
import com.xy.service.DeliverToTeacherService;
import com.xy.service.impl.DeliverToStudentServiceImpl;
import com.xy.service.impl.DeliverToTeacherServiceImpl;
import com.xy.service.impl.TextBookServiceImpl;

/**
 * Servlet implementation class DeliverToTeacherServlet
 */
@WebServlet("/deliverToTeacherServlet")
public class DeliverToTeacherServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeliverToTeacherServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		DeliverToTeacherService service = new DeliverToTeacherServiceImpl();
		
		if("add".equals(request.getParameter("status"))) {
			int id = Integer.valueOf(request.getParameter("id"));//textbookId
			TextBook tb = new TextBookServiceImpl().get(id);
			DeliverToTeacher dtt = new DeliverToTeacher();
			dtt.setAdmin((Admin) request.getSession().getAttribute("admin"));
			dtt.setAmount(Integer.valueOf(request.getParameter("amount")));
			dtt.setCreateDate(new Date());
			dtt.setDeliverTime(new Date());
			dtt.setName(request.getParameter("name"));
			dtt.setTextBook(tb);
			service.add(dtt);
			request.getRequestDispatcher("/pages/public/main.jsp").forward(request, response);
		}else  if("list".equals(request.getParameter("status"))) {
			request.setAttribute("dtts", service.list(DeliverToTeacher.class));
			request.getRequestDispatcher("/pages/storage/storage001/teacher/deliver_query.jsp").forward(request, response);
		}else if("query".equals(request.getParameter("status"))) {
			String start = request.getParameter("start");
			String end = request.getParameter("end");
			String name = request.getParameter("name") == null? "%":request.getParameter("name");
			if(name==null||name.equals("null")) {
				name="%";
			}
			String hql = "from DeliverToTeacher where deliverTime>='"+start+"'"
					+" and deliverTime<='"+end+"'"
					+" and name like '%"+name+"%'";
			List<DeliverToTeacher> dtts = new DeliverToTeacherServiceImpl().queryByHql(hql);
			request.setAttribute("dtts", dtts);
			request.getRequestDispatcher("/pages/storage/storage001/teacher/deliver_query.jsp").forward(request, response);
		}
			
	}

}
